#*--------------------------------------------------------------------------------
# DESCRIPTION: Creating Figure 7 
#              (Entering and Exiting the 2008 Financial Crisis)
#
#--------------------------------------------------------------------------------#

install.packages(c("fect","haven", "fixest", "ggplot2", "extrafont", "dplyr", "stringr", "flextable", "officer"))

library(dplyr)
library(extrafont)
library(fect)
library(fixest)
library(ggplot2)
library(haven)
library(stringr)
library(flextable)
library(officer)

font_import()
loadfonts(device = "win")

##############################################################################################################
##SET RELEVANT WORKING DIRECTORY
setwd("...") 

##############################################################################################################
##PREPARE DATA
data <- read_dta("data/clean/country_year_panel.dta")
df.use <- data[!is.na(data$growth_shock_mpd),]
df.use <- data.frame(df.use)
df.use <- get.cohort(data = df.use, D = "growth_shock_mpd",index = c("panelid","year"))

##############################################################################################################
##RUN MODELS
model.fect <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                   method = "fe", index = c("panelid","year"), se = TRUE, nboots = 200, alpha = 0.05, 
                   parallel = TRUE, seed = 1234, force = "two-way", group = "Cohort")

model.ife <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                  index = c("panelid","year"), force = "two-way", method = "ife", CV = TRUE, r = c(0, 4), alpha = 0.05, 
                  se = TRUE, nboots = 200, parallel = TRUE, seed = 1234, group = "Cohort")

model.mc <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                 index = c("panelid","year"), force = "two-way", method = "mc", CV = TRUE, alpha = 0.05, 
                 se = TRUE, nboots = 200, parallel = TRUE, seed = 1234, group = "Cohort")

##############################################################################################################
##LEFT-HAND PANEL (ENTERING TREATMENT)
ci_bounds <- model.fect[["est.group.output"]][["Cohort:2009"]][["att.on.bound"]]
att <- model.fect[["group.output"]][["Cohort:2009"]][["att.on"]]
time <- model.fect[["group.output"]][["Cohort:2009"]][["time.on"]]
fect.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "FE"
)

ci_bounds <- model.ife[["est.group.output"]][["Cohort:2009"]][["att.on.bound"]]
att <- model.ife[["group.output"]][["Cohort:2009"]][["att.on"]]
time <- model.ife[["group.output"]][["Cohort:2009"]][["time.on"]]
ife.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "IFE"
)

ci_bounds <- model.mc[["est.group.output"]][["Cohort:2009"]][["att.on.bound"]]
att <- model.mc[["group.output"]][["Cohort:2009"]][["att.on"]]
time <- model.mc[["group.output"]][["Cohort:2009"]][["time.on"]]
mc.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "MC"
)

output <- rbind(fect.output, ife.output, mc.output)

figure_7a <- ggplot(output, aes(x = Time, y = ATT, group = Model)) +
  geom_hline(yintercept = 0, colour = "firebrick4") +
  geom_vline(xintercept = 0, colour = "firebrick4", linetype = 'dashed') +
  geom_errorbar(aes(ymin = CI.lower,
                    ymax = CI.upper, color = Model), position = position_dodge(width=0.5)) + 
  geom_point(aes(shape = Model, fill = Model),  size=1.75, color = "black", position = position_dodge(width=0.5)) + 
  theme(text = element_text(family = "Palatino"),
        panel.background = element_rect(fill = 'white', color = 'black'),
        panel.grid.major = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        panel.grid.minor = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12),
        title=element_text(size=13),
        plot.caption = element_text(hjust=0)) + 
  coord_cartesian(xlim=c(-3.1,3.1)) + ylim(-0.25,0.15) + scale_x_continuous(breaks=seq(-4, 3, 1)) + 
  ylab("Effect on Trust in Government") + 
  xlab("Time to 2008 Financial Crisis") +
  scale_color_manual(values=c('firebrick4','darkgrey','steelblue4')) +
  scale_fill_manual(values=c('firebrick4','darkgrey','steelblue4')) +
  scale_shape_manual(values = c(21, 23, 22)) + guides(col = "none")

ggsave(
  filename = "output/figures/main/figure_7a.png",
  plot = figure_7a,
  width = 6,
  height = 5,
  units = "in",
  dpi = 600
)

ggsave(
  filename = "output/figures/main/figure_7a.tif",
  plot = figure_7a,
  width = 6,
  height = 5,
  units = "in",
  dpi = 600,
  device = "tiff",
  compression = "lzw"
)

##############################################################################################################
##RIGHT-HAND PANEL (EXITING TREATMENT)
ci_bounds <- model.fect[["est.group.output"]][["Cohort:2009"]][["att.off.bound"]]
att <- model.fect[["group.output"]][["Cohort:2009"]][["att.off"]]
time <- model.fect[["group.output"]][["Cohort:2009"]][["time.off"]]
fect.output.exit <- data.frame(
  Time = time,
  ATT.OFF = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "FE"
)

ci_bounds <- model.ife[["est.group.output"]][["Cohort:2009"]][["att.off.bound"]]
att <- model.ife[["group.output"]][["Cohort:2009"]][["att.off"]]
time <- model.ife[["group.output"]][["Cohort:2009"]][["time.off"]]
ife.output.exit <- data.frame(
  Time = time,
  ATT.OFF = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "IFE"
)

ci_bounds <- model.mc[["est.group.output"]][["Cohort:2009"]][["att.off.bound"]]
att <- model.mc[["group.output"]][["Cohort:2009"]][["att.off"]]
time <- model.mc[["group.output"]][["Cohort:2009"]][["time.off"]]
mc.output.exit <- data.frame(
  Time = time,
  ATT.OFF = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "MC"
)

output.exit <- rbind(fect.output.exit, ife.output.exit, mc.output.exit)

figure_7b <- ggplot(output.exit, aes(x = Time, y = ATT.OFF, group = Model)) +
  geom_hline(yintercept = 0, colour = "firebrick4") +
  geom_vline(xintercept = 0, colour = "firebrick4", linetype = 'dashed') +
  geom_errorbar(aes(ymin = CI.lower,
                    ymax = CI.upper, color = Model), position = position_dodge(width=0.5)) + 
  geom_point(aes(shape = Model, fill = Model),  size=1.75, color = "black", position = position_dodge(width=0.5)) + 
  theme(text = element_text(family = "Palatino"),
        panel.background = element_rect(fill = 'white', color = 'black'),
        panel.grid.major = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        panel.grid.minor = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12),
        title=element_text(size=13),
        plot.caption = element_text(hjust=0)) + 
  coord_cartesian(xlim=c(-2.1,4.1)) + ylim(-0.25,0.15) + scale_x_continuous(breaks=seq(-2, 4, 1)) + 
  ylab("Effect on Trust in Government") + 
  xlab("Time to Exiting 2008 Financial Crisis") +
  scale_color_manual(values=c('firebrick4','darkgrey','steelblue4')) +
  scale_fill_manual(values=c('firebrick4','darkgrey','steelblue4')) +
  scale_shape_manual(values = c(21, 23, 22)) + guides(col = "none")

ggsave(
  filename = "output/figures/main/figure_7b.png",
  plot = figure_7b,
  width = 6,
  height = 5,
  units = "in",
  dpi = 600
)

ggsave(
  filename = "output/figures/main/figure_7b.tif",
  plot = figure_7b,
  width = 6,
  height = 5,
  units = "in",
  dpi = 600,
  device = "tiff",
  compression = "lzw"
)
